package com.lovo.manage_back.controller;

import com.lovo.manage_back.aspect.Log;
import com.lovo.manage_back.bean.User;
import com.lovo.manage_back.bean.form.UserForm;
import com.lovo.manage_back.result.GlobalHandleException;
import com.lovo.manage_back.result.ResponseResult;
import com.lovo.manage_back.result.ResultCode;
import com.lovo.manage_back.result.UserValid;
import com.lovo.manage_back.service.UserService;
import com.lovo.manage_back.util.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;

@RestController
@ResponseResult
@RequestMapping("/users")
@Tag(name = "UserController用户管理")
@SecurityRequirement(name = "bearer-key")
public class UserController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserService userService;
    @GetMapping
    @Operation(summary = "查询用户")
    @UserValid
//    @Log("查询用户")
    public Object listUser(UserForm userForm){
        User user = new User();
        user.setUsername(userForm.getUsername());
        return userService.listUser(userForm.getCurrent(),userForm.getPageSize(),user);
    }
    @GetMapping("/roles/{id}")
    @Operation(summary = "查询用户分配的角色")
    public Object listUserRole(@PathVariable Integer id){
        return userService.listUserRole(id);
    }
    @PostMapping("/login")
    @Operation(summary = "登录")
//    @Log("登录")
    public Object login(@RequestBody UserForm userForm) throws GlobalHandleException {
        User user = new User();
        BeanUtils.copyProperties(userForm,user);
        user.setPwd(DigestUtils.sha256Hex(user.getPwd()));
        user = userService.login(user);
        if(user == null){
            throw new GlobalHandleException(ResultCode.USER_LOGIN_ERROR);
        }else{
            String token = JWTUtil.createToken(user.getUsername(),user.getPwd(), Calendar.MINUTE,30);
//            request.setAttribute("user",user);
            return token;
        }

//        UsernamePasswordToken token = new UsernamePasswordToken(userForm.getUsername(),DigestUtils.sha256Hex(userForm.getPwd()));
//        Subject loginUser = SecurityUtils.getSubject();
//        try{
//            loginUser.login(token);
//        }catch(IncorrectCredentialsException e){
//            throw new GlobalHandleException(ResultCode.USER_PASS_ERROR);
//        }catch(UnknownAccountException e){
//            throw new GlobalHandleException(ResultCode.USER_NOT_EXIST);
//        }
//        return loginUser.getPrincipal();


    }
    @PutMapping("/assignRoles/{id}")
    @Operation(summary = "给用户分配角色")
    public Object assignRoles(@PathVariable Integer id,@RequestBody UserForm userForm){
        return userService.assignUserRole(id,userForm.getRoleIds());
    }

    @PutMapping ("/{id}")
    @Operation(summary = "根据用户ID修改")
    public Object updateUser(@PathVariable Integer id,@RequestBody UserForm userForm){
        User user = new User();
        BeanUtils.copyProperties(userForm,user);
        user.setId(id);
        return userService.updateById(user);
    }
    @DeleteMapping("/{id}")
    @Operation(summary = "根据用户ID删除")
    public Object deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id);
    }
}
